import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login' // 设置默认重定向到 /home 页面
  },
  //登录注册
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.../views/LoginRegisterView/LoginView.vue
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginRegisterView/LoginView.vue'),
  }
  , {
    path: '/seekerregister', // 定义带参数的路由
    name: 'seekerregister',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginRegisterView/SeekerRegisterView.vue'),
  },
  {
    path: '/companyregister', // 定义带参数的路由
    name: 'companyregister',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginRegisterView/CompanyRegisterView.vue'),
  },
  {
    path: '/manage-login', // 定义带参数的路由
    name: 'manage-login',
    component: () => import(/* webpackChunkName: "about" */ '../views/LoginRegisterView/ManaLoginView.vue'),
  },
  //管理员
  {
    path: '/manage', // 定义带参数的路由
    name: 'manage',
    component: () => import(/* webpackChunkName: "about" */ '../views/ManagerViews/ManagerViews.vue'),
    meta: { requiresAuthMana: true }
  },
  {
    path: '/manage-recruitment', // 定义带参数的路由
    name: 'manage-recruitment',
    component: () => import(/* webpackChunkName: "about" */ '../views/ManagerViews/ManaRecruitViews.vue'),
    meta: { requiresAuthMana: true }
  },
  {
    path: '/manage-company', // 定义带参数的路由
    name: 'manage-company',
    component: () => import(/* webpackChunkName: "about" */ '../views/ManagerViews/ManaComViews.vue'),
    meta: { requiresAuthMana: true }

  },
  {
    path: '/manage-position', // 定义带参数的路由
    name: 'manage-position',
    component: () => import(/* webpackChunkName: "about" */ '../views/ManagerViews/ManaPosViews.vue'),
    meta: { requiresAuthMana: true }
  },
  // 公司
  {
    path: '/company-posmanage', // 定义带参数的路由
    name: 'company-posmanage',
    component: () => import(/* webpackChunkName: "about" */ '../views/CompanyViews/CompanyPosManaView.vue'),
    meta: { requiresAuthCom: true }
  },
  {
    path: '/company-message', // 定义带参数的路由
    name: 'company-message',
    component: () => import(/* webpackChunkName: "about" */ '../views/CompanyViews/ComMessageView.vue'),
    meta: { requiresAuthCom: true }
  },
  {
    path: '/company-seeker', // 定义带参数的路由
    name: 'company-seeker',
    component: () => import(/* webpackChunkName: "about" */ '../views/CompanyViews/ComSeekerView.vue'),
    meta: { requiresAuthCom: true }
  },
  {
    path: '/company-recruit/:ElemPos', // 定义带参数的路由
    name: 'company-recruit',
    component: () => import(/* webpackChunkName: "about" */ '../views/CompanyViews/ComRecruitSeekerView.vue'),
    meta: { requiresAuthCom: true }
  },
  //公司在线聊天
  {
    path: '/ComChat', // 定义带参数的路由
    name: 'ComChat',
    component: () => import(/* webpackChunkName: "about" */ '../views/CompanyViews/ComChatView.vue'),
    meta: { requiresAuthCom: true }
  },
  // 用户
  {
    path: '/seekerHomePage', // 定义带参数的路由
    name: 'seekerHomePage',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/HomeView/SeekerHomeView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/seekerRecomPage', // 定义带参数的路由
    name: 'seekerRecomPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/HomeView/SeekerRecomView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/seekerComPage', // 定义带参数的路由
    name: 'seekerComPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/HomeView/SeekerComView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/seekerAppPage', // 定义带参数的路由
    name: 'seekerAppPage',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/HomeView/SeekerAppView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  //个人中心
  {
    path: '/PerRevisePassword', // 定义带参数的路由
    name: 'PerRevisePassword',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/PersonView/SeekerPerPasswordView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/PerWriteCv', // 定义带参数的路由
    name: 'PerWriteCv',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/PersonView/SeekerWriteCvView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/PerDeliver', // 定义带参数的路由
    name: 'PerDeliver',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/PersonView/SeekerDeliverView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  //用户在线聊天
  {
    path: '/PerChat', // 定义带参数的路由
    name: 'PerChat',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/PersonView/SeekerChatview.vue'),
    meta: { requiresAuthSeeker: true }
  },
  //公司和职位的具体描述
  {
    path: '/Posdescribe/:ElemSeekerPos/:Cinfo', // 定义带参数的路由
    name: 'Posdescribe',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/DescribeView/PosDescribeView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/Comdescribe', // 定义带参数的路由
    name: 'Comdescribe',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/DescribeView/ComDescribeView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  //搜索
  {
    path: '/SearchCom', // 定义带参数的路由
    name: 'SearchCom',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/SearchView/SearchComView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  {
    path: '/SearchPos', // 定义带参数的路由
    name: 'SearchPos',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/SearchView/SearchPosView.vue'),
    meta: { requiresAuthSeeker: true }
  },
  //岗位分类
  {
    path: '/PosSort', // 定义带参数的路由
    name: 'PosSort',
    component: () => import(/* webpackChunkName: "about" */ '../views/SeekerViews/PosSortView/PosSortComView.vue'),
    meta: { requiresAuthSeeker: true }
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 用户路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = JSON.parse(sessionStorage.getItem('LoginSeekerState'));
  // 检查目标路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuthSeeker)) {
    if (!isAuthenticated) {
      next('/login'); // 未认证用户重定向到登录页面
    } else {
      next(); // 认证用户允许访问
    }
  } else {
    next(); // 其他路由直接放行
  }
});


// 公司路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = JSON.parse(sessionStorage.getItem('LoginComState'));
  // 检查目标路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuthCom)) {
    if (!isAuthenticated) {
      next('/login'); // 未认证用户重定向到登录页面
    } else {
      next(); // 认证用户允许访问
    }
  } else {
    next(); // 其他路由直接放行
  }
});


// 管理员路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = JSON.parse(sessionStorage.getItem('LoginManaState'));
  // 检查目标路由是否需要认证
  if (to.matched.some(record => record.meta.requiresAuthMana)) {
    if (!isAuthenticated) {
      next('/manage-login'); // 未认证用户重定向到登录页面
    } else {
      next(); // 认证用户允许访问
    }
  } else {
    next(); // 其他路由直接放行
  }
});



export default router
